package com.aipanda.entity;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * 支付方式类
 * @param 	name			支付方式名称
 * @param	type			支付类型[在线支付，线下支付]
 * @param	timeout			超时时间
 * @param	icon			图标
 * @param	description		介绍	
 * @param	content			内容
 * @param	shippingMethods	支持配送方式	[多对多关系]
 * @param	orders			订单			[一对多关系]
 * @author wanyuxiang
 *
 */
@Entity
@Table(name = "aipanda_payment_method")
public class PaymentMethod extends OrderEntity {
	private static final long serialVersionUID = 6949816500116581199L;
	private String name;
	private Type type;
	private Integer timeout;
	private String icon;
	private String description;
	private String content;
	private Set<ShippingMethod> shippingMethods = new HashSet<ShippingMethod>();
	private Set<Order> orders = new HashSet<Order>();

	public enum Type {
		online, offline;
	}

	@NotEmpty
	@Length(max = 255)
	@Column(nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@NotNull
	@Column(nullable = false)
	public Type getType() {
		return this.type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	@Min(1L)
	public Integer getTimeout() {
		return this.timeout;
	}

	public void setTimeout(Integer timeout) {
		this.timeout = timeout;
	}

	@Length(max = 255)
	public String getIcon() {
		return this.icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	@Length(max = 255)
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Lob
	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "aipanda_payment_shipping_method",joinColumns ={@JoinColumn(name="payment_methods",nullable=false)},inverseJoinColumns = {@JoinColumn(name="shipping_methods",nullable=false)})
	@OrderBy("order asc")
	public Set<ShippingMethod> getShippingMethods() {
		return this.shippingMethods;
	}

	public void setShippingMethods(Set<ShippingMethod> shippingMethods) {
		this.shippingMethods = shippingMethods;
	}

	@OneToMany(mappedBy = "paymentMethod", fetch = FetchType.LAZY)
	public Set<Order> getOrders() {
		return this.orders;
	}

	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	}

	public PaymentMethod preRemove(PaymentMethod paymentMethod) {
		Set<Order> set = paymentMethod.getOrders();
		if (set != null) {
			Iterator<Order> orders = set.iterator();
			while (orders.hasNext()) {
				Order order = orders.next();
				order.setPaymentMethod(null);
			}
		}
		return paymentMethod;
	}
}
